/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc. 
 *  
 * SPDX-License-Identifier: Apache-2.0.
 * 
 * @Date: 2021-07-01 18:40:52
 * @LastEditTime: 2021-08-17 11:34:21
 * @Description:  This files is for 
 * 
 * @Modify History: 
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 * 1.0   Huanghe     2021/7/3     init
 * 1.1   Zhugengyu   2021/8/3     unify a32 and a64 timer api
 */

#include "fsleep.h"
#include "generic_timer.h"
#include "kernel.h"
#include "parameters.h"
#include "ft_types.h"

static u32 fsleep_general(u32 ticks, u32 div)
{
    u64 end_time;
    u64 cur_time;
    GenericTimerStart();
    cur_time = GenericTimerRead();
    end_time = cur_time + ((u64)ticks * GenericTimerFrequecy() / div);

    do
    {
        cur_time = GenericTimerRead();
    } while (cur_time < end_time);

    return 0;
}

u32 fsleep_seconds(u32 seconds)
{
    return fsleep_general(seconds, 1);
}

u32 fsleep_millisec(u32 mseconds)
{
    return fsleep_general(mseconds, NANO_TO_MICRO);
}

u32 fsleep_microsec(u32 mseconds)
{
    return fsleep_general(mseconds, NANO_TO_KILO);
}